package ilearn.job.disserver.order.controller;

import ilearn.job.disserver.order.entity.Orders;
import ilearn.job.disserver.order.service.OrdersService;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Log4j2
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    @GetMapping("/add")
    public Orders add(String accId, String goods, String desc, BigDecimal amt){
        return ordersService.add(accId,goods,desc,amt);
    }

    @GetMapping("/getOrdersByAccId")
    public List<Orders> getOrdersByAccId(String accId){
        return ordersService.getOrdersByAccId(accId);
    }

    @ExceptionHandler(value = RuntimeException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Object runtimeExceptionHandler(RuntimeException runtimeException){
        Map<String,Object> map = new HashMap<>();
        map.put("msg",runtimeException.getLocalizedMessage());
        log.error(runtimeException);
        return map;
    }
}
